package net.hotelOrderSystem.service;

import net.hotelOrderSystem.domain.Order;
import net.hotelOrderSystem.domain.OrderExample;
import net.hotelOrderSystem.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl  implements OrderService{
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public List<Order> findOrder() {
        return orderMapper.selectByExample(new OrderExample());
    }

    @Override
    public List<Order> findOrderByAccount(String account) {
        OrderExample orderExample =new OrderExample();
        orderExample.createCriteria().andAccountEqualTo(account);
        return  orderMapper.selectByExample(orderExample);
    }

    @Override
    public void saveOrder(Order order) {
        if(orderMapper.selectByPrimaryKey(order.getOrderId()) == null) {
            orderMapper.insert(order);
        } else {
            orderMapper.updateByPrimaryKey(order);
        }
    }

    @Override
    public void deleteOrderByID(String orderId) {
        OrderExample order = new OrderExample();
        order.createCriteria().andOrderIdEqualTo(orderId);
        orderMapper.deleteByExample(order);
    }

    @Override
    public void updateOrderByID(String orderId, Order order) {
        OrderExample order1 = new OrderExample();
        order1.createCriteria().andOrderIdEqualTo(orderId);
        orderMapper.updateByExample(order,order1);
    }

    @Override
    public Order findOrderByID(String orderId) {
        OrderExample order = new OrderExample();
        order.createCriteria().andOrderIdEqualTo(orderId);
        return orderMapper.selectByExample(order).get(0);
    }

    @Override
    public List<Order> findOrderByHotelId(String hotelName) {
        OrderExample orderExample =new OrderExample();
        orderExample.createCriteria().andHotelNameEqualTo(hotelName);
        return orderMapper.selectByExample(orderExample);
    }
}
